package genetic;

public abstract class Individual implements Cloneable, Comparable<Object> {
	protected double x[];

	//Quando mais proximo de zero, melhor
	public abstract Double fitness();

	public abstract void normalize();
	
	//Modifica levemente esse individuo
	public void mutate(){
		for (int i = 0; i < x.length;i++) {
			if (Math.random() < 0.5)
				x[i] = x[i] + RandomUtils.between(-0.3, 0.3);
		}
		normalize();
	}

	@Override 
	public Individual clone() {
		try {
			Individual clone = (Individual) super.clone();
			x = x.clone();
			return clone;
		} catch (CloneNotSupportedException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public int compareTo(Object o){
		Individual b = (Individual)o;
		return (int)(this.fitness() - b.fitness());	
	}

}